﻿$(function () {
    var tempRequestID = $('input[type="hidden"]#serviceRequestID').val();
    //撤销表单弹出框
    $("input[type='button'].btnUndo").click(function () {
        if (confirm("您确定要撤销该服务申请?")) {
            var docheight = $(document).height();
            //追加一个层，使背景变灰
            $("#greybackground").css({ "opacity": "0.5", "height": docheight });
            $("#greybackground").show();
            $("#popupWindowTitle").attr("class", "undo").html("撤销对话框");
            $("#suggestionAlias").html("撤销意见");
            $("div.popupWindowSuggestion").show("slow");
        }
    });
    //拒绝表单弹出框
    $("input[type='button'].btnReject").click(function () {
        if (confirm("您确定要拒绝该服务申请?")) {
            var docheight = $(document).height();
            //追加一个层，使背景变灰
            $("#greybackground").css({ "opacity": "0.5", "height": docheight });
            $("#greybackground").show();
            $("#popupWindowTitle").attr("class", "reject").html("拒绝对话框");
            $("#suggestionAlias").html("拒绝意见");
            $("div.popupWindowSuggestion").show("slow");
        }
    });
    //同意表单弹出框
    $("input[type='button'].btnApprove").click(function () {
        if (confirm("您同意通过该服务申请?")) {
            var rnd = (new Date()).getMilliseconds() + "" + parseInt(Math.random() * 10000);
            $.ajax({
                url: "isDirector",
                type: "GET",
                data: { "serviceRequestID": tempRequestID, "nocache": rnd },
                dataType: "json",
                success: function (data) {
                    var docheight = $(document).height();
                    //追加一个层，使背景变灰
                    $("#greybackground").css({ "opacity": "0.5", "height": docheight });
                    $("#greybackground").show();
                    $("#popupWindowTitle").attr("class", "approve").html("同意对话框");
                    $("#suggestionAlias").html("意见");
                    if (data.isDirector == 0) {
                        $("tr.suggestionNewArea td.tdName").html("主任审批");
                        $("tr.suggestionNewArea").show();
                        //                        $("#suggestionAreaTable tbody").append("<tr class='suggestionNewArea'><td width='30%' class='tdName'>主任审批</td><td class='tdData'> <input type='checkbox' id='needDirtorApprove' /></td></tr>");
                    } else if (data.isDirector == 2) {
                        $("tr.suggestionNewArea td.tdName").html("提交到省");
                        $("tr.suggestionNewArea").show();
                        //                        $("#suggestionAreaTable tbody").append("<tr class='suggestionNewArea'><td width='30%' class='tdName'>提交到省</td><td class='tdData'> <input type='checkbox' id='needDirtorApprove' /></td></tr>");
                    }
                    $("div.popupWindowSuggestion").show("slow");
                }
            });
        }
    });
    //保存意见
    $("input[type='button']#btnSaveSuggestion").click(function () {
        var action = $("#popupWindowTitle").attr("class");
        switch (action) {
            case "undo":
                undoHandler();
                break;
            case "approve":
                approveHandler();
                break;
            case "reject":
                rejectHandler();
                break;
            default:
                break;
        }
    });
    //取消
    $("input[type='button']#btnCancelSuggestion").click(function () {
        var desc = $('#txtSuggestionDesc').val('');
        $("tr.suggestionNewArea").hide();
        $("#greybackground").css({ "z-index": "1001" });
        $("div.popupWindowSuggestion").hide();
        $("div.popupWindow").hide();
        $("#greybackground").hide();
    });
    //意见弹出框
    $("input[type='button'].btnViewSuggestion").click(function () {
        var rnd = (new Date()).getMilliseconds() + "" + parseInt(Math.random() * 10000);
        $.ajax({
            url: "getHistoryByserviceRequestID",
            type: "GET",
            dataType: "json",
            data: { "serviceRequestID": tempRequestID, "nocache": rnd },
            success: function (data) {
                if (data.status == 0) {
                    alert('加载审批意见失败，请稍后再试！');
                } else {
                    $("#btnAddSuggestion").remove();
                    $("#tbViewSuggestions tbody").empty();
                    //                    if (data.status == 2) {
                    //                        $("div.popupWindow td.btnFields").prepend('<input class="Button1 ui-icons" id="btnAddSuggestion" title="添加意见" value="添加意见" type="button" />');
                    //                    }
                    $.each(data.data, function (i, item) {
                        $("#tbViewSuggestions tbody").append("<tr><td width='30%' class='tdName'>" + item.approverName + "<br/>" + ConvertJSONDateToJSDateObject(item.approveDate) + "</td><td class='tdData'>" + item.comments + "</td></tr>");
                    });
                    var docheight = $(document).height();
                    //追加一个层，使背景变灰
                    $("#greybackground").css({ "opacity": "0.5", "height": docheight });
                    $("#greybackground").show();
                    $("div.popupWindow").show("slow");
                }
            }
        });
    });
    function undoHandler() {
        var desc = $('#txtSuggestionDesc').val();
        var rnd = (new Date()).getMilliseconds() + "" + parseInt(Math.random() * 10000);
        $.ajax({
            url: "undoServiceRequest",
            type: "POST",
            dataType: "json",
            data: { "serviceRequestID": tempRequestID, "desc": desc, "nocache": rnd },
            success: function (data) {
                if (data.status == 1) {
                    alert('撤销服务申请成功！');
                    window.location = "/ITService/Index";
                } else {
                    alert("撤销服务申请失败，请稍后再试!");
                }
                $("div.popupWindowSuggestion").hide();
                $("#greybackground").hide();
            }
        });
    }
    function approveHandler() {
        var aa = $("#needDirtorApprove").attr('checked');
        var rnd = (new Date()).getMilliseconds() + "" + parseInt(Math.random() * 10000);
        $.ajax({
            url: "approveServiceRequest",
            type: "POST",
            dataType: "json",
            data: { "serviceRequestID": tempRequestID, "needDirector": $("#needDirtorApprove").attr('checked'), "approveNote": $("#txtSuggestionDesc").val(), "nocace": rnd },
            success: function (data) {
                if (data.status == 1) {
                    alert('操作已成功！');
                    window.location.href = window.location.href;
                } else {
                    alert("操作失败，请稍后再试!");
                }
                $("div.popupWindowSuggestion").hide();
                $("div#greybackground").hide();
            }
        });
    }
    function rejectHandler() {
        var desc = $('#txtSuggestionDesc').val();
        if ($.trim(desc) == '') {
            alert("请填写拒绝意见！");
            return false;
        }
        var rnd = (new Date()).getMilliseconds() + "" + parseInt(Math.random() * 10000);
        $.ajax({
            url: "rejectServiceRequest",
            type: "POST",
            dataType: "json",
            data: { "serviceRequestID": tempRequestID, "desc": desc, "nocache": rnd },
            success: function (data) {
                if (data.status == 1) {
                    alert('操作已成功！');
                    window.location.href = window.location.href;
                } else {
                    alert("操作失败，请稍后再试!");
                }
                $("div.popupWindowSuggestion").hide();
                $("#greybackground").hide();
            }
        });
    }
})